﻿using MediatR;
using S.RBAC.Api.Application.Command.StaffCommand;
using S.RBAC.Domain.Dto;
using S.RBAC.Infrastructure.Interface;

namespace S.RBAC.Api.Application.CommandHandlers.StaffHandler
{
    public class StaffLoginCommandHandler : IRequestHandler<StaffLoginCommand, OutputLoginDto>
    {
        protected readonly IStaffRepository _staff;

        public StaffLoginCommandHandler(IStaffRepository staff)
        {
            _staff = staff;
        }

        public async Task<OutputLoginDto> Handle(StaffLoginCommand request, CancellationToken cancellationToken)
        {
            var info = await _staff.GetStaffList(t => t.AccountNumber == request.AccountNumber && t.Password == request.Password);

            var list = info.FirstOrDefault();

            if (list != null)
            {
                return new OutputLoginDto { StaffId = list.StaffId, Token = null, Msg = "登录成功！" };
            }

            return new OutputLoginDto { Msg = "账号或密码错误，请重新输入！" };
        }
    }
}
